package mr.stocks.support;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;


public class ChartBuilder {
    
    public static void main(String[] args) throws Exception {
        File outputFile = new File("./target/sampleImg.png");
        List<StockAverage> data = Arrays.asList(
             new StockAverage("ABC", "2007", 1.1f),
             new StockAverage("ABC", "2008", 1.5f),
             new StockAverage("ABC", "2009", 2.3f),
             new StockAverage("ABC", "2010", 4.5f),
             new StockAverage("ABC", "2011", 3.6f)
        );
        
        ChartBuilder chartBuilder = new ChartBuilder("Stock", "Year", "Dividents", data);
        final JFreeChart chart = chartBuilder.build();
        BufferedImage img = chart.createBufferedImage(1200, 600);
        ChartUtilities.writeBufferedImageAsPNG(new FileOutputStream(outputFile), img);
        System.out.println("Created Chart [" + outputFile + "]");
    }
    
    
    private String title;
    private String xAxistLabel;
    private String yAxisLable;
    private List<StockAverage> divs;
    
    public ChartBuilder(String title, String xAxistLabel, String yAxisLable, List<StockAverage> divs) {
        this.title = title;
        this.xAxistLabel = xAxistLabel;
        this.yAxisLable = yAxisLable;
        this.divs = divs;
    }

    public JFreeChart build(){
        Collections.sort(divs, new Comparator<StockAverage>() {
            @Override
            public int compare(StockAverage o1, StockAverage o2) {
                return o1.getYear().compareTo(o2.getYear());
            }
        });
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (StockAverage d : divs){
            dataset.addValue(d.getAverage(), d.getName(), d.getYear());
        }
        
        final JFreeChart chart = ChartFactory.createLineChart(
                title,       // chart title
                xAxistLabel,                    // domain axis label
                yAxisLable,                   // range axis label
                dataset,                   // data
                PlotOrientation.VERTICAL,  // orientation
                false,                      // include legend
                false,                      // tooltips
                false                      // urls
            );
            chart.setBackgroundPaint(Color.white);

            final CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.lightGray);
            plot.setRangeGridlinePaint(Color.white);

            // customise the range axis...
            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            rangeAxis.setAutoRangeIncludesZero(true);
            
            // customise the renderer...
            final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
            renderer.setSeriesStroke(
                0, new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {10.0f, 6.0f}, 0.0f
                )
            );
            renderer.setSeriesStroke(
                1, new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {6.0f, 6.0f}, 0.0f
                )
            );
            renderer.setSeriesStroke(
                2, new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {2.0f, 6.0f}, 0.0f
                )
            );
            return chart;
    }

    public String getTitle() {
        return title;
    }



    public void setTitle(String title) {
        this.title = title;
    }



    public String getxAxistLabel() {
        return xAxistLabel;
    }



    public void setxAxistLabel(String xAxistLabel) {
        this.xAxistLabel = xAxistLabel;
    }



    public String getyAxisLable() {
        return yAxisLable;
    }



    public void setyAxisLable(String yAxisLable) {
        this.yAxisLable = yAxisLable;
    }



    public List<StockAverage> getDivs() {
        return divs;
    }



    public void setDivs(List<StockAverage> divs) {
        this.divs = divs;
    }
}
